import request from '@/service'

export const getCaptcha = (data) => {
  return request({
    url: '/blade-auth/oauth/captcha',
    method: 'get',
    data
  })
}
//手机验证码
export const getVrificationCode = (param) => {
  return request({
    url: '/blade-auth/oauth/send-validate',
    method: 'post',
    param
  })
}

/**
 * 判断用户是否需要设置密码
 * @param {String} params.username 手机号
 * @param {String} params.tenantId 租户
 * @return {Object}
 */
export const checkFirstLogin = ({ tenantId, username }) => {
  return request({
    url: '/blade-auth/oauth/check-username',
    method: 'get',
    headers: {
      'Tenant-Id': tenantId
    },
    data: { username },
    extraHeader: {
      noAuthorization: true
    }
  })
}


/**
 * 忘记密码，重新设置密码
 * @param {String} params.key 验证码 id
 * @param {String} params.vCode 验证码
 * @param {String} params.username 手机号
 * @param {String} params.password 密码
 * @param {String} params.password1 密码
 * @param {String} params.id 短信 id
 * @param {String} params.value 短信验证码
 * @return {Object}
 */
export const resetPassword = ({ key, vCode, username, password, id, value, password1 }) => {
  return request({
    url: '/blade-auth/oauth/reset-password',
    method: 'post',
    headers: {
      'Captcha-Key': key,
      'Captcha-Code': vCode
    },
    extraHeader: {
      noAuthorization: true
    },
    params: {
      username,
      password,
      id,
      value,
      password1
    }
  })
}


/**
 * 用户账号密码登录、用户验证码登录
 */
export function loginByUsername({ tenantId, deptId, roleId, username, password, type, key, vCode, grantType, id, value, password1, resetPasswordType }) {
  return request({
    method: 'post',
    url: `/blade-auth/oauth/token`,
    isAuth: true,
    header: {
      'Tenant-Id': tenantId,
      'Dept-Id':  '',
      'Role-Id':  '',
      'Captcha-Key': key,
      'Captcha-Code': vCode
    },
    param: {
      grant_type: grantType,
      scope: 'all',
      tenantId,
      username,
      password,
      type,
      id,
      value

    }
  })
}